package com.generalbackend.role.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.generalbackend.role.enums.AllRoleApiEnum;
import com.generalbackend.role.enums.CommonApiEnum;
import com.generalbackend.role.mapper.RestApiMapper;
import com.generalbackend.role.model.entity.RestApi;
import com.generalbackend.role.model.query.RestApiQuery;
import com.generalbackend.role.service.IRestApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.generalbackend.common.constants.Constants.DATA;
import static com.generalbackend.common.constants.Constants.TOTAL;

/**
* @Description 接口表业务层实现类
* @author tim.xie
* @date 2021-12-14 13:19
*/

@Service
public class RestApiServiceImpl extends ServiceImpl<RestApiMapper, RestApi> implements IRestApiService {

    private List<RestApi> commonApiCache = new ArrayList<>();
    private List<RestApi> allRoleApiCache = new ArrayList<>();
    private Map<String, List<RestApi>> userApiCache = new HashMap<>();

	@Autowired
	private RestApiMapper restApiMapper;

	@Override
	public Boolean insert(RestApi restApi) {
        Integer num = restApiMapper.insert(restApi);
        if(num <= 0) {
            return false;
        }
        refreshCache();
		return true;
	}

	@Override
	public Boolean update(RestApi restApi) {
        Integer num = restApiMapper.updateById(restApi);
        if(num <= 0) {
          return false;
        }
        refreshCache();
		return true;
	}

	@Override
	public Boolean deleteById(String id) {
        restApiMapper.deleteById(id);
        refreshCache();
		return true;
	}

	@Override
	public RestApi selectById(String id) {
        RestApi restApi = restApiMapper.selectById(id);
        return restApi;
	}
    @Override
    public JSONObject list(RestApiQuery restApiQuery) {
        JSONObject result = new JSONObject();
        Page<RestApi> page = new Page<>(restApiQuery.getOffset(), restApiQuery.getLimit());
        List<RestApi> restApiList = restApiMapper.list(page,restApiQuery);
        result.put(TOTAL,  page.getTotal());
        result.put(DATA, restApiList);
        return result;
    }

    @Override
    public List<RestApi> getCommonApi() {
        LambdaQueryWrapper<RestApi> queryWrapper = new LambdaQueryWrapper<RestApi>()
                .eq(RestApi::getIsCommon, CommonApiEnum.COMMON.code());
        return restApiMapper.selectList(queryWrapper);
    }

    @Override
    public List<RestApi> getAllRoleApi() {
        LambdaQueryWrapper<RestApi> queryWrapper = new LambdaQueryWrapper<RestApi>()
                .eq(RestApi::getIsAll, AllRoleApiEnum.ALL_ROLE.code());
        return restApiMapper.selectList(queryWrapper);
    }

    /**
     * 刷新缓存
     */
    public void refreshCache() {
	    userApiCache.clear();
	    commonApiCache.clear();
	    allRoleApiCache.clear();
	    commonApiCache = getCommonApi();
	    allRoleApiCache = getAllRoleApi();
    }

    @Override
    public RestApi selectByApi(String api) {
        LambdaQueryWrapper<RestApi> queryWrapper = new LambdaQueryWrapper<RestApi>()
                .eq(RestApi::getUrl, api).last("limit 1");
        return restApiMapper.selectOne(queryWrapper);
    }

    @Override
    public List<RestApi> selectByMenuList(List<String> menuList) {
        if (menuList == null || menuList.isEmpty()) {
            return null;
        }
        String menus = String.join(",", menuList);
        if (userApiCache.containsKey(menus)) {
            return userApiCache.get(menus);
        } else {
            List<RestApi> restApiList = restApiMapper.selectByMenuList(menuList);
            userApiCache.put(menus, restApiList);
            return restApiList;
        }
    }


}
